//
//  ECUITVCell_CourseOrder.m
//  Edu_iOS2018
//
//  Created by yanyan on 2018/1/11.
//  Copyright © 2018年 yanyan. All rights reserved.
//

#import "ECUITVCell_CourseOrder.h"
#import "UIView_Helper.h"
#import "ECCourseOrderModel.h"
#import "ECData_Format.h"
@interface ECUIView_CourseOrder()

@end
@implementation ECUIView_CourseOrder
- (void)addSubviews
{
    [super addSubviews];
    _courseImageView = [UIView_Helper yImageView];
    _courseImageView.contentMode = UIViewContentModeScaleToFill;
    self.courseImageView.layer.cornerRadius = kCourseImageCornerRadius;
    self.courseImageView.layer.masksToBounds = YES;
    [self addSubview:_courseImageView];
    
    _courseNameLabel = [UIView_Helper yLabel];
    _courseNameLabel.font = [UIFont systemFontOfSize:13*kPercentage];
    _courseNameLabel.textColor = [UIColor colorWithHexString:@"#040404"];
    [self addSubview:_courseNameLabel];
    
    _courseTeacherLabel = [UIView_Helper yLabel];
    _courseTeacherLabel.font = [UIFont systemFontOfSize:12];
    _courseTeacherLabel.textColor = [UIColor colorWithHexString:@"#808080"];
    [self addSubview:_courseTeacherLabel];
    _priceLabel = [UIView_Helper yLabel];
    _priceLabel.font = [UIFont systemFontOfSize:14];
    _priceLabel.textColor = [UIColor colorWithHexString:@"#ef4023"];
    [self addSubview:_priceLabel];
    WeakSelf(weakSelf);
    CGFloat padding = 10;
    [self.courseImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(padding));
        make.top.equalTo(self.mas_top).offset(kProportion_Height(42));
        make.width.equalTo(@(kProportion_Width(324)));
        make.height.equalTo(weakSelf.courseImageView.mas_width).multipliedBy(0.5);
        make.centerY.equalTo(weakSelf);
        
    }];
    [self.courseNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.courseImageView.mas_top);
        make.left.equalTo(weakSelf.courseImageView.mas_right).offset(padding);
        make.right.equalTo(weakSelf).offset(-5);
    }];
    [self.courseTeacherLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.courseNameLabel.mas_bottom);
        make.left.equalTo(weakSelf.courseNameLabel);
        make.centerY.equalTo(weakSelf);
        make.right.equalTo(self.courseNameLabel);
    }];
    [self.priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.courseTeacherLabel.mas_bottom).offset(5);
        make.left.equalTo(weakSelf.courseNameLabel);
        make.bottom.equalTo(weakSelf.courseImageView);
        make.right.equalTo(weakSelf.courseNameLabel);
    }];
}
- (void)setObject:(id)object
{
    [super setObject:object];
}
@end

//////
@interface ECUITVCell_CourseOrder()

@property(nonatomic,strong)UILabel *orderIdLabel;//订单号
@property(nonatomic,strong)UILabel *stateLabel;//订单状态
@property(nonatomic,strong)ECUIView_CourseOrder *courseView;
@property(nonatomic,strong)UILabel *truePriceLabel;//课程实际花费的价格
@property(nonatomic,strong)UIButton *cancelBtn;//取消订单按钮
@property(nonatomic,strong)UIButton *payBtn;//立即支付按钮
@end
@implementation ECUITVCell_CourseOrder

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}
- (void)addSubviews
{
    [super addSubviews];
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    _orderIdLabel = [UIView_Helper yLabel];
    _orderIdLabel.textColor = [UIColor colorWithHexString:@"#999999"];
    _orderIdLabel.font = [UIFont systemFontOfSize:12];
    [self.contentView addSubview:_orderIdLabel];
    //
    _stateLabel = [UIView_Helper yLabel];
    _stateLabel.layer.cornerRadius = kProportion_Height(60)/2.0;
    _stateLabel.layer.masksToBounds = YES;
    _stateLabel.textAlignment = NSTextAlignmentCenter;
    _stateLabel.font = [UIFont systemFontOfSize:12];
    _stateLabel.backgroundColor = [UIColor colorWithHex:0xfff0e5];
    _stateLabel.textColor = ECTHEMESELECTEDCOLOR;
    [self.contentView addSubview:_stateLabel];
    //
    _courseView = [[ECUIView_CourseOrder alloc]init];
    [_courseView setBackgroundColor: [UIColor colorWithHexString:@"#f8f8f8"]];
    [self.contentView addSubview:_courseView];
    
    _truePriceLabel = [UIView_Helper yLabel];
    _truePriceLabel.textColor = [UIColor colorWithHexString:@"#1a1a1a"];
    _truePriceLabel.font = [UIFont systemFontOfSize:14];
    _truePriceLabel.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:_truePriceLabel];
    _cancelBtn = [UIView_Helper yButton];
    _cancelBtn.layer.cornerRadius = 5;
    _cancelBtn.layer.masksToBounds = YES;
    _cancelBtn.layer.borderColor = [[UIColor colorWithHexString:@"#dddddd"]CGColor];
    _cancelBtn.layer.borderWidth = 0.5;
    [_cancelBtn setTitleColor:[UIColor colorWithHexString:@"#333333"] forState:UIControlStateNormal];
    [_cancelBtn setTitle:@"取消订单" forState:UIControlStateNormal];
    _cancelBtn.titleLabel.font = [UIFont systemFontOfSize:13];
    [_cancelBtn addTarget:self action:@selector(action_cancel:) forControlEvents:UIControlEventTouchUpInside];

    [self.contentView  addSubview:_cancelBtn];
    _payBtn = [UIView_Helper yButton];
    [_payBtn setTitleColor:[UIColor colorWithHexString:@"#ef4023"] forState:UIControlStateNormal];//279af7
    [_payBtn setTitle:@"立即支付" forState:UIControlStateNormal];
    _payBtn.layer.cornerRadius = 5;
    _payBtn.layer.masksToBounds = YES;
    _payBtn.titleLabel.font = [UIFont systemFontOfSize:13];
    _payBtn.layer.borderColor = [[UIColor colorWithHexString:@"#ef4023"]CGColor];
    _payBtn.layer.borderWidth = 0.5;
    [_payBtn addTarget:self action:@selector(action_paid:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:_payBtn];
    UIView *bottomView = [UIView_Helper yView];
    bottomView.backgroundColor = [UIColor colorWithHexString:@"#f5f5f5"];
    [self.contentView addSubview:bottomView];
    CGFloat padding_Left = 10;
    CGFloat padding = 8;

    WeakSelf(weakSelf);
    
    [self.orderIdLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).offset(padding_Left);
        make.top.equalTo(self.contentView);
        make.height.equalTo(@(kProportion_Height(108)));
    }];
    [self.stateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(weakSelf.contentView).offset(-padding_Left);
        make.centerY.equalTo(weakSelf.orderIdLabel);
        make.width.equalTo(@(kProportion_Width(198)));
        make.height.equalTo(@(kProportion_Height(60)));
    }];
    [_courseView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.contentView);
        make.top.equalTo(weakSelf.orderIdLabel.mas_bottom);
        make.width.equalTo(weakSelf.contentView);
        make.height.equalTo(@(kProportion_Height(246)));
    }];

    [self.truePriceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.orderIdLabel);
        make.top.equalTo(weakSelf.courseView.mas_bottom);
        make.height.equalTo(@(kProportion_Height(142)));

    }];
    [self.payBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(weakSelf.contentView).offset(-10);
        make.centerY.equalTo(weakSelf.truePriceLabel);
        make.width.equalTo(@(kProportion_Width(198)));
        make.height.equalTo(@(kProportion_Width(90)));

//        make.height.equalTo(weakSelf.payBtn.mas_width).multipliedBy(0.5);
    }];
    [self.cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(weakSelf.payBtn.mas_left).offset(-10);
        make.centerY.equalTo(weakSelf.truePriceLabel);
        make.width.equalTo(weakSelf.payBtn);
        make.height.equalTo(weakSelf.payBtn);
    }];
    [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.contentView);
        make.top.greaterThanOrEqualTo(weakSelf.truePriceLabel.mas_bottom);
        make.height.equalTo(@(10));
        make.right.equalTo(weakSelf.contentView.mas_right);
        make.bottom.equalTo(weakSelf.contentView);
    }];
    
}




- (void)setObject:(id)object
{
    [super setObject:object];
    ECCourseOrderModel *mo = object;
    self.stateLabel.text = [ECData_Format orderStateStringForState:mo];
    _cancelBtn.hidden = NO;
    _payBtn.hidden = NO;
    if ([mo.orderState isEqualToString:@"cancelled"]) {
        _stateLabel.backgroundColor = [UIColor colorWithHex:0xfff0e5];
        _stateLabel.textColor = ECTHEMESELECTEDCOLOR;
        _stateLabel.text = @"已取消";
        _cancelBtn.hidden = YES;
        _payBtn.hidden = YES;
    }
    if ([mo.orderState isEqualToString:@"created"]) {
        //待支付
        _stateLabel.backgroundColor = [UIColor colorWithHex:0xfff0e5];
        _stateLabel.textColor = ECTHEMESELECTEDCOLOR;
        _cancelBtn.layer.borderColor = [[UIColor colorWithHexString:@"#dddddd"]CGColor];
        [_cancelBtn setTitleColor:[UIColor colorWithHexString:@"#333333"] forState:UIControlStateNormal];
        [_cancelBtn setTitle:@"取消订单" forState:UIControlStateNormal];
        [_payBtn setTitleColor:[UIColor colorWithHexString:@"#ef4023"] forState:UIControlStateNormal];//279af7
        [_payBtn setTitle:@"立即支付" forState:UIControlStateNormal];
        _payBtn.layer.borderColor = [[UIColor colorWithHexString:@"#ef4023"]CGColor];
    }
    if ([mo.orderState isEqualToString:@"paid"]) {
        if (mo.reviewed) {
            //已完成
            _stateLabel.backgroundColor = [UIColor colorWithHex:0xf5f5f5];
            _stateLabel.textColor = [UIColor colorWithHexString:@"#1a1a1a"];
            _cancelBtn.hidden = YES;
            _payBtn.hidden = YES;
//            [_payBtn setTitleColor:[UIColor colorWithHexString:@"#279af7"] forState:UIControlStateNormal];//279af7
//            [_payBtn setTitle:@"查看评价" forState:UIControlStateNormal];
//            _payBtn.layer.borderColor = [[UIColor colorWithHexString:@"#dddddd"]CGColor];
        } else{
            //待评价
            _stateLabel.backgroundColor = [UIColor colorWithHex:0xe9f5fe];
            _stateLabel.textColor = [UIColor colorWithHexString:@"#279af7"];
            _cancelBtn.hidden = YES;
            [_payBtn setTitleColor:[UIColor colorWithHexString:@"#279af7"] forState:UIControlStateNormal];//279af7
            _payBtn.layer.borderColor = [[UIColor colorWithHexString:@"#dddddd"]CGColor];
            [_payBtn setTitle:@"去评价" forState:UIControlStateNormal];
        }
    
    }
    self.orderIdLabel.text = [NSString stringWithFormat:@"订单号: %@",mo.orderSN];
    [self.courseView.courseImageView sd_setImageWithURL:[NSURL URLWithString:mo.mo_course.pictureUrl] placeholderImage:[UIImage imageNamed:@"goods_order_placeholder"]];
    self.courseView.courseNameLabel.text = mo.mo_course.title;
    self.courseView.courseTeacherLabel.text = [NSString stringWithFormat:@"任课老师: %@",mo.mo_course.teacher];
    self.courseView.priceLabel.attributedText = [ECFormatHelper attributedStringForPromotionPrice:mo.mo_course.price decimal:2 smallFontSize:10];
    self.truePriceLabel.text = [NSString stringWithFormat:@"合计: %@",mo.totalPrice];
}
-(void)action_paid:(id)sender{

    if (self.block_right) {
        self.block_right(self.object);
    }
   
}
-(void)action_cancel:(id)sender{
    
    if (self.block_left) {
        self.block_left(self.object);
    }
    
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    // Configure the view for the selected state
}

@end
